package ajax;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.GeoModel;

import data.GeoData;

@SuppressWarnings("serial")
public class WardAjaxServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		String id = (String) request.getParameter("id");
		String action = (String) request.getParameter("action");
		ArrayList<GeoModel> cities = new ArrayList<GeoModel>();
		if (!id.isEmpty()) {
			GeoData geoData = new GeoData();
			cities = geoData.getChildrenById(id);
		}
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		String result = "<option value=\"\">Phường/Xã</option>";
		if (action != null && action.equalsIgnoreCase("route")) {
			result = "";
		}
		for (int i = 0; i < cities.size(); i++) {
			result += "<option value=\"" + cities.get(i).getGeoId() + "\">"
					+ cities.get(i).getName() + "</option>";
		}

		response.getWriter().println(result);
	}
}
